package dp;

/**
 * Created by fengliejv on 2017/12/4.
 */
//todo use 中心扩展
public class PalindromicSubstrings647 {
    public int countSubstrings(String s) {
        int result = s.length();
        boolean dp[][] = new boolean[s.length()][s.length()];
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = true;
        }
        for (int i = 1; i < s.length(); i++) {
            for (int j = 0; j < s.length() - i; j++) {
                if (s.charAt(j) == s.charAt(j + i)) {
                    if (j + 1 <= j + i - 1 && dp[j + 1][j + i - 1] || j + 1 > j + i - 1) {
                        dp[j][j + i] = true;
                        result++;
                    }
                } else {
                    dp[j][j + i] = false;
                }
            }
        }
        return result;
    }
}
